﻿
<div class="ld-tabs">
    <ul>
        <li class="k-state-active">单位系数</li>
    </ul>
    <div>
        <div class="k-block divToolbar">
            <div style="display: inline-block;">
                <input id="ddlProduct"  />
            </div>
            <div style="display: inline-block;">
                <input id="ddlMaterial" />
            </div>
            <div style="display: inline-block;">
                <input id="ddlSize" />
            </div>
            <button class="k-button" onclick="Filters()"><span class="k-icon k-i-search"></span><span>搜索</span></button>
            <button class="k-button" onclick="CreateNew()"><span class="k-icon k-i-plus"></span><span>添加新系数</span></button>
        </div>
        <div id="grid_ratios" style="text-align: center"></div>
    </div>
</div>

<script class="init">
    LocatedMenu("产品目录", "单位系数")

    var products = Whale.Remote.GetEntity({
        url: "/Products/Product/Gets",
        parameters: {
            pageSize: 0,
            productStatus: 1,
            productType: 2
        }
    });
    var materials = Whale.Remote.GetEntity({
        url: "/Products/Material/Gets",
        parameters: {
            mType: 2
        }
    });
    var sizes = Whale.Remote.GetEntity({
        url: "/Products/Size/Gets",
        parameters: {
            pType: 2
        }
    });

    $("#ddlProduct").kendoComboBox({
        placeholder: "请选择产品...",
        filter: "contains",
        dataSource: products,
        dataTextField: "Module",
        dataValueField: "ID",
        select: function (e) {
            var item = this.dataItem(e.item.index());
            $("#ddlProduct").attr("data-value", item.ID);
        },
        change: function (e) {
            if (!$.isNumeric(this.value())) {
                this.text("");
                $("#ddlProduct").attr("data-value", 0);
            }
        }
    });

    $("#ddlMaterial").kendoDropDownList({
        optionLabel: {
            MaterialName: "请选择材料...",
            ID: 0
        },
        dataTextField: "MaterialName",
        dataValueField: "ID",
        dataSource: materials
    });

    $("#ddlSize").kendoDropDownList({
        optionLabel: {
            SizeName: "请选择规格...",
            ID: 0
        },
        dataTextField: "SizeName",
        dataValueField: "ID",
        dataSource: sizes
    });
</script>

<script class="grid_ratio_columns">
    var ratioFields = [{
        title: "#",
        field: "ID",
        width: 50
    }, {
        title: "产品",
        field: "Module",
        width: 150
    }, {
        title: "材料",
        field: "MaterialName",
        width: 100
    }, {
        title: "规格",
        field: "Size",
        width: 100
    }, {
        title: "系数",
        field: "Ratio",
        width: 200
    }, {
        title: "操作",
        template: "#=GetOperations(ID)#",
        attributes: { style: "text-align:left" }
    }];

    function GetOperations(id) {
        var html = "<button class='k-button' onclick=\"Edit(" + id + ")\"><span class='k-icon k-i-pencil'></span><span>修改</span></button>";
        html += "<button class='k-button' onclick=\"Delete(" + id + ")\"><span class='k-icon k-i-close'></span><span>删除</span></button>";
        return html;
    }

    function Edit(id) {
        if (wndGeneralEdit != null && typeof wndGeneralEdit != "undefined") {
            wndGeneralEdit.setOptions({ title: "修改单位系数" });
            wndGeneralEdit.refresh({ url: "/Products/Ratio/Edit/" + id });
            wndGeneralEdit.open();
        }
    }

    function Delete(id) {
        if (confirm("真的要删除这个系数吗?")) {
            Whale.Remote.Operation({
                url: "/Products/Ratio/Delete",
                parameters: { ratioID: id },
                success: function (result) {
                        $("#grid_ratios").data("kendoGrid").dataSource.read();
                },
                error: function (msg) {
                    wndGeneralInformation.content(msg).open();
                }
            });
        }
    }
</script>

<script class="grid_config">
    $(function () {
        $("#grid_ratios").kendoGrid({
            columns: ratioFields,
            dataSource: Whale.dataSource({
                url: "/Products/Ratio/Gets",
                pageSize: 20,
                getParameters: function (e) {
                    return {
                        productID: $("#ddlProduct").attr("data-value"),
                        materialID: $("#ddlMaterial").data("kendoDropDownList").value(),
                        size: $("#ddlSize").data("kendoDropDownList").value() > 0 ? $("#ddlSize").data("kendoDropDownList").text() : "",
                    }
                }
            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            }
        });
    });

    function CreateNew() {
        wndGeneralEdit.setOptions({ title: "添加新的系数" });
        wndGeneralEdit.refresh({ url: "/Products/Ratio/Edit" });
        wndGeneralEdit.open();
    }

    function Filters() {
        $("#grid_ratios").data("kendoGrid").dataSource.page(0);
    }
</script>